package com.Q03;

import java.util.ArrayList;
import java.util.Stack;

/**
 * @version v1.0.0
 * 创建时间：2022/6/27 17:14
 */
public class Q03_03 {
    public static void main(String[] args) {
        StackOfPlates st = new StackOfPlates(1);
        st.push(1);
        st.push(2);

        st.popAt(1);
        st.pop();
        st.pop();
    }
}
class StackOfPlates {
    int size ;
    ArrayList<Stack<Integer>> stackList;
    public StackOfPlates(int cap) {
        size = cap;
        stackList = new ArrayList<>();
    }

    public void push(int val) {
        if(this.size<=0) return;

        if(stackList.isEmpty() || stackList.get(stackList.size()-1).size()==this.size){
            stackList.add(new Stack<Integer>());
        }
        if(stackList.get(stackList.size()-1).size()<this.size){
            stackList.get(stackList.size()-1).push(val);
        }
    }

    public int pop() {
        return popAt(stackList.size()-1);
    }

    public int popAt(int index) {
        // 如果index 边界超过界线返回-1
        if (index < 0 || index >= stackList.size()) {
            return -1;
        }
        // 如果待删除的stack是空的,返回-1
        Stack<Integer> stack = stackList.get(index);
        if (stack.isEmpty()){
            return -1; // 栈没有元素的情况pop
        }

        int res = stack.pop();
        if (stack.isEmpty()) {
            stackList.remove(index);
        }
        return res;
    }
}

/**
 * Your StackOfPlates object will be instantiated and called as such:
 * StackOfPlates obj = new StackOfPlates(cap);
 * obj.push(val);
 * int param_2 = obj.pop();
 * int param_3 = obj.popAt(index);
 */